package com.engineering.elink.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.CourseAddCo;
import com.engineering.elink.co.CourseCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.domain.Course;
import com.engineering.elink.domain.CourseBrowse;
import com.engineering.elink.domain.KeyWord;
import com.engineering.elink.domain.Label;
import com.engineering.elink.domain.ThirdCategory;
import com.engineering.elink.domain.VideoSecondaryCategory;
import com.engineering.elink.domain.VideoTopCategory;
import com.engineering.elink.service.CourseService;
import com.engineering.elink.util.ResJsonUtil;
import com.engineering.elink.videodao.CourseBrowseMapper;
import com.engineering.elink.videodao.CourseMapper;
import com.engineering.elink.videodao.CourseTypeMapper;
import com.engineering.elink.videodao.EnrollMapper;
import com.engineering.elink.videodao.KeyWordMapper;
import com.engineering.elink.videodao.LabelMapper;
import com.engineering.elink.videodao.ThirdCategoryMapper;
import com.engineering.elink.videodao.UserViewMapper;
import com.engineering.elink.videodao.VideoSecondaryCategoryMapper;
import com.engineering.elink.videodao.VideoTopCategoryMapper;
import com.engineering.elink.vo.CourseVo;
import com.engineering.elink.vo.TeacherVo;
import com.engineering.elink.vo.UserVo;
import com.engineering.elink.xaservice.EvaluateXaService;

@Service
public class CourseServiceImpl implements CourseService{
	
	private static Logger logger = Logger.getLogger(CourseServiceImpl.class);
	
	@Autowired
	private CourseMapper courseMapper;
	
	@Autowired
	private LabelMapper labelMapper;
	
	@Autowired
	private KeyWordMapper keyWordMapper;
	
	@Autowired
	private CourseBrowseMapper courseBrowseMapper;
	
	@Autowired
	private VideoTopCategoryMapper videoTopCategoryMapper;
	
	@Autowired
	private VideoSecondaryCategoryMapper videoSecondaryCategoryMapper;
	
	@Autowired
	private ThirdCategoryMapper thirdCategoryMapper;
	
	@Autowired
	private UserViewMapper userViewMapper;
	
	@Autowired
	private EvaluateXaService evaluateXaService;
	
	@Autowired
	private CourseTypeMapper courseTypeMapper;
	
	@Autowired
	private EnrollMapper enrollMapper;

	@Override
	public Map<String, Object> getCoursesList(CourseCo co,String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<CourseVo> list = courseMapper.selectList(co,userId);
		for (CourseVo courseVo : list) {
			List<Label> labelList = labelMapper.getLabelList(courseVo.getLabelIds());
			courseVo.setLabelList(labelList);
		}
		map.put("list", list);
		map.put("Pagination", new Pagination(co, courseMapper.selectCount(co,userId)));
		
		//课程文件所有分类
		map.put("courseTypeList",courseTypeMapper.selectList());
		
		//未分类数
//		if(co.getType()!=0){
//			map.put("notTypeCount", 0);
//		}else{
			map.put("notTypeCount", courseMapper.selectCountNotType());
		//}

		// 将关键词存入表中
		KeyWord kw = keyWordMapper.selectByKeyWord(co.getKeyWord(),0);
		
		if (kw != null) {// 表示该关键词存在
			kw.setSearchCount(kw.getSearchCount() + 1);
			keyWordMapper.updateByPrimaryKeySelective(kw);
		} else {
			if (co.getKeyWord() != null && co.getKeyWord() != "") {
				keyWordMapper.insertSelective(new KeyWord(co.getKeyWord(),1,0));
			}
		}
		return map;
	}
	
	@Override
	public Map<String,Object> add(CourseAddCo addCo,String userId) {
		logger.info("发布课程开始！！！");
		try {
			if(addCo.getAgreeFlag()==null || addCo.getAgreeFlag().intValue()==0){
				return ResJsonUtil.getMap(ErrorType.COURSE_SERVICE_ERROR);
			}
			return ResJsonUtil.getMap(ErrorType.SUCCESS,evaluateXaService.saveCourse(addCo, userId));
			
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.COURSE_ADD_ERROR,e.getMessage());
		} 	
	}
	
	
	@Override
	public Map<String,Object> update(Integer courseId,CourseAddCo addCo,String userId) {
		//审核通过才允许发布吧
		//课程已发布还允许修改课程开始时间和结束时间吗？
		try {
			Course course=courseMapper.selectByPrimaryKey(courseId);
			if(course==null)
				return ResJsonUtil.getMap(ErrorType.COURSE_ERROR);
			
			if(addCo.getAgreeFlag()==null || addCo.getAgreeFlag().intValue()==0){
				return ResJsonUtil.getMap(ErrorType.COURSE_SERVICE_ERROR);
			}
			evaluateXaService.updateCourse(course,addCo, userId);
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return ResJsonUtil.getMap(ErrorType.COURSE_ADD_ERROR);

	}

	@Override
	public Map<String, Object> getHotCourse(BaseCo co) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", courseMapper.getHotCourse(co));
		
		map.put("Pagination", new Pagination(co, courseMapper.selectCountHotCourse()));
		
		return map;
	}

	@Override
	public Map<String, Object> getBrowseCourse(BaseCo co) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", courseMapper.getBrowseCourse(co));
		
		map.put("Pagination", new Pagination(co, courseMapper.selectCountBrowseCourse()));
		
		return map;
	}

	@Override
	public Map<String, Object> get(Integer courseId, String userId) {
		TeacherVo teacherVo = courseMapper.selectInfo(courseId, userId);
		if (teacherVo != null) {
			List<Label> labelList = labelMapper.getLabelList(teacherVo.getLabelIds());
			teacherVo.setLabelList(labelList);

			// 往课程浏览表插入数据
			courseBrowseMapper.insertSelective(new CourseBrowse(courseId,userId));
			
			//更新课程浏览次数
			Course course=courseMapper.selectByPrimaryKey(courseId);
			course.setVisitCount(course.getVisitCount()+1);
			courseMapper.updateByPrimaryKeySelective(course);

			//查询该课程的分类
			VideoTopCategory top = this.videoTopCategoryMapper.selectByPrimaryKey(teacherVo.getTcId());
			VideoSecondaryCategory sec = videoSecondaryCategoryMapper.selectByPrimaryKey(teacherVo.getScId());
			ThirdCategory  third = thirdCategoryMapper.selectByPrimaryKey(teacherVo.getThirdId());
			Map<String, Object> category = new HashMap<String, Object>();
			category.put("topCategory", top);
			category.put("secCategory", sec);
			category.put("thirdCategory", third);
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("course", teacherVo);
			map.put("category", category);
			
			//该讲师的视频个数
			UserVo user=teacherVo.getTeacher();
			if(user==null){
				map.put("teacherCount", 0);
			}else{
				map.put("teacherCount", courseMapper.selectTeacherCount(user.getUserId()));
			}
			
			//是否报名
			map.put("enrollCount",enrollMapper.selectCountEnroll(courseId, userId));
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS, map);

		} else {
			return ResJsonUtil.getMap(ErrorType.COURSE_ERROR);
		}
	}

	@Override
	public Map<String, Object> getTeacherCourse(BaseCo co, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		BaseCo bco=new BaseCo();
		bco.setAll(true);
		bco.setPageSize(6);
		
		map.put("list", courseMapper.getTeacherCourse(bco, userId));
		
		return map;
	}

	@Override
	public Map<String, Object> getSameCourse(BaseCo co,Integer courseId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", courseMapper.getSameCourse(co,courseId));
		map.put("Pagination", new Pagination(co, courseMapper.countSameCourse(courseId)));
		
		return map;
	}


	@Override
	public Map<String, Object> getMyCourse(BaseCo co, String userId) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", courseMapper.getMyCourse(co, userId));
		
		map.put("Pagination", new Pagination(co, courseMapper.selectCountMyCourse(userId)));
		
		return map;
	}
	
	
	@Override
	public Map<String, Object> getTeacherCourseList(BaseCo co) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", userViewMapper.selectUserView(co)); 
		
		map.put("Pagination", new Pagination(co, userViewMapper.selectCountUserView()));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> updateType(Integer courseId, Integer typeId) {
		Course course=courseMapper.selectByPrimaryKey(courseId);
		if(course==null)
			return ResJsonUtil.getMap(ErrorType.COURSE_ERROR);
		
		course.setType(typeId);
		
		courseMapper.updateByPrimaryKeySelective(course);
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}
	
}
